//
//  DataFMDBHandle.m
//  礼物go
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DataFMDBHandle.h"


@implementation DataFMDBHandle

+(DataFMDBHandle *)shareDataFMDBHandle{
    static DataFMDBHandle *data = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        data = [[DataFMDBHandle alloc] init];
    });
    return data;
}

-(void)openDB{
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",filePath);
    NSString *dataPath = [filePath stringByAppendingPathComponent:@"FMDB.sqlite"];
    self.dbQueue = [FMDatabaseQueue databaseQueueWithPath:dataPath];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db open];
        if (result) {
            NSLog(@"数据库打开成功");
        } else {
            NSLog(@"数据库打开失败");
        }
    }];
}

-(void)createLoginTable{
    NSString *sql = @"create table if not exists login(number integer primary key autoincrement, account text, password text)";
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"注册表创建成功");
        } else {
            NSLog(@"注册表创建失败");
        }
    }];
}

-(void)insertLoginWithAccount:(NSString *)account Password:(NSString *)password{
    NSString *sql = [NSString stringWithFormat:@"insert into login(account, password) values('%@', '%@')",account,password];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"注册表插入成功");
        } else{
            NSLog(@"注册表插入失败");
        }
    }];
}

-(NSMutableArray *)selectAllAccounts{
    NSString *sql = [NSString stringWithFormat:@"select * from login"];
    NSMutableArray *accountArray = [NSMutableArray array];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            NSString *account = [result objectForColumnName:@"account"];
            [accountArray addObject:account];
        }
        
    }];
    return  accountArray;
}

-(NSString *)selectAccount:(NSString *)account{
    NSString *sql = [NSString stringWithFormat:@"select * from login where account = '%@'",account];
    __block NSString *password = @"0";
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            password = [result objectForColumnName:@"password"];
        }
    }];
    return password;
}

-(void)updateAccount:(NSString *)Image WithAccount:(NSString *)account{
    NSString *sql = [NSString stringWithFormat:@"update login set image = '%@' where account = '%@'",Image,account];
  [self.dbQueue inDatabase:^(FMDatabase *db) {
      BOOL result = [db executeUpdate:sql];
      if (result) {
          NSLog(@"图片更新成功");
      } else {
          NSLog(@"图片更新失败");
      }
  }];
}

///////////////////////////

-(void)createHotColumnTable{
    NSString *sql = @"create table if not exists hotColumn(number integer primary key autoincrement, cover_image_url text, favorites_count text, myId text, name text, price text, account text)";
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"热门表创建成功");
        } else{
            NSLog(@"热门表创建失败");
        }
    }];
 
}


-(void)insertHotColumn:(HotDetailModel *)model WithAccount:(NSString *)account{
    NSString *sql = [NSString stringWithFormat:@"insert into hotColumn(cover_image_url, favorites_count, myId, name, price, account) values('%@','%@','%@','%@','%@','%@')",model.cover_image_url,model.favorites_count,model.myId,model.name,model.price,account];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"热门推荐插入成功");
        } else {
            NSLog(@"热门推荐插入失败");
        }
    }];
}

-(void)deleteHotcolumn:(NSString *)account myId:(NSNumber *)myId{
    NSString *sql = [NSString stringWithFormat:@"delete from hotColumn where account = '%@' and myId = '%@'",account,myId];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"删除热门推荐成功");
        } else {
            NSLog(@"删除热门推荐失败");
        }
    }];

}

-(NSMutableArray *)selecthotColumn:(NSString *)account WithModelOrNot:(BOOL)YOrN{
    NSString *sql = [NSString stringWithFormat:@"select * from hotColumn where account = '%@'",account];
    NSMutableArray *modelArray = [NSMutableArray array];
     NSMutableArray *titleArray = [NSMutableArray array];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
    FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            HotDetailModel *model = [[HotDetailModel alloc] init];
            model.cover_image_url = [result objectForColumnName:@"cover_image_url"];
           NSInteger favorites_count = [[result objectForColumnName:@"favorites_count"] integerValue];
            model.favorites_count = @(favorites_count);
//            NSInteger myId = [[result objectForColumnName:@"myId"] integerValue];
            model.myId = [NSNumber numberWithInteger:[[result objectForColumnName:@"myId"] integerValue]];
            model.name = [result objectForColumnName:@"name"];
            model.price = [result objectForColumnName:@"price"];
            
            [modelArray addObject:model];
            [titleArray addObject:model.name];
        }
    }];
    if (YOrN) {
        return modelArray;
    } else {
        return titleArray;
    }

}

//判断id是否存在
- (BOOL)selectDataWithmyId:(NSNumber *)myId WithAccount:(NSString *)account{
    NSString *selectSQL = [NSString stringWithFormat:@"select * from hotColumn where myId = '%@' and account = '%@'", myId,account];
   
   __block BOOL myBool = NO;
    // 执行查询
   [self.dbQueue inDatabase:^(FMDatabase *db) {
       FMResultSet *resultSet = [db executeQuery:selectSQL];
       while ([resultSet next]) {
           NSLog(@"数据库中已存在");
           myBool = YES;
           break;
       }
       
   }];
    
    return myBool;
    
}




/////////////////////////////////////
-(void)createPresentTable{
NSString *sql = @"create table if not exists present(number integer primary key autoincrement, cover_image_url text, title text, myId text, account text)";
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"攻略表创建成功");
        } else{
            NSLog(@"攻略表创建失败");
        }
    }];
}

-(void)insertPresent:(SelectDetailModel *)model WithAccount:(NSString *)account{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"insert into present(cover_image_url, title, myId, account) values('%@','%@','%@','%@')",model.cover_image_url,model.title,model.myId,account];
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"攻略表插入成功");
        } else{
            NSLog(@"攻略表插入失败");
        }
    }];
}

-(void)insertPresentContentModel:(SelectContentModel *)model WithAccount:(NSString *)account{
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        NSString *sql = [NSString stringWithFormat:@"insert into present(cover_image_url, title, myId, account) values('%@','%@','%@','%@')",model.cover_image_url,model.title,model.myId,account];
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"攻略表插入成功");
        } else{
            NSLog(@"攻略表插入失败");
        }
    }];
}




-(void)deletePresent:(NSString *)Account MyId:(NSNumber *)myId{
    NSString *sql = [NSString stringWithFormat:@"delete from present where account = '%@' and myId = '%@'",Account,myId];
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        BOOL result = [db executeUpdate:sql];
        if (result) {
            NSLog(@"攻略表删除成功");
        } else{
            NSLog(@"攻略表删除失败");
        }
    }];
}

-(NSMutableArray *)selectPrsent:(NSString *)account WithModelOrNot:(BOOL)YOrN{
 NSMutableArray *modelArray = [NSMutableArray array];
 NSMutableArray *titleArray = [NSMutableArray array];
  [self.dbQueue inDatabase:^(FMDatabase *db) {
      NSString *sql = [NSString stringWithFormat:@"select * from present where account = '%@'",account];
      FMResultSet *result = [db executeQuery:sql];
      while ([result next]) {
          SelectContentModel *model =[[SelectContentModel alloc] init];
          model.cover_image_url = [result objectForColumnName:@"cover_image_url"];
          model.title = [result objectForColumnName:@"title"];
          model.myId = [NSNumber numberWithInteger:[[result objectForColumnName:@"myId"] integerValue]];
        
          [modelArray addObject:model];
          [titleArray addObject:model.title];
      }
 }];

    if (YOrN) {
        return modelArray;
    } else {
        return titleArray;
    }

}


//判断id是否存在
- (BOOL)presentDataWithId:(NSNumber *)myId WithAccount:(NSString *)account{
    NSString *selectSQL = [NSString stringWithFormat:@"select * from present where myId = '%@' and account = '%@'", myId,account];
    
    __block BOOL myBool = NO;
    // 执行查询
    [self.dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultSet = [db executeQuery:selectSQL];
        while ([resultSet next]) {
            NSLog(@"数据库中已存在");
            myBool = YES;
            break;
        }
        
    }];
    
    return myBool;
    
}







@end
